package com.rocksdata.mc.business;

import com.ctz.commonframe.utils.MD5;
import com.ctz.commonframe.utils.SharedPrefrencesUtil;
import com.rocksdata.mc.BuildConfig;
import com.rocksdata.mc.SharedPreferencesKey;
import com.rocksdata.mc.database.UserInfoDBManager;
import com.rocksdata.mc.entity.StudentContactInfo;
import com.rocksdata.mc.entity.StudentInfo;
import com.rocksdata.mc.entity.TeacherInfo;
import com.rocksdata.mc.http.HttpClient;
import com.rocksdata.mc.database.DatabaseManager;
import com.rocksdata.mc.entity.TeacherContactInfo;
import com.rocksdata.mc.entity.UserInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 陈堂正 on 2017/4/6.
 */

public class UserInfoManager implements BusinessInterface.IUserInfoManager {
    public static final int USER_TYPE_STUDENT = 2;
    public static final int USER_TYPE_TEACHER = 1;

    private static final String URL_GET_STUDENT_LIST = BuildConfig.HOST + "/user/students";
    private static final String URL_GET_TEACHER_LIST = BuildConfig.HOST + "/user/staffs";
    private static final String URL_GET_STUDENT_INFO = BuildConfig.HOST + "/user/getStudentByUserID";
    private static final String URL_GET_TEACHER_INFO = BuildConfig.HOST + "/user/getStaffByUserID";

    private static BusinessInterface.IUserInfoManager instance = null;
    private static UserInfoDBManager dbManager;

    private UserInfoManager(){

    }

    private static void initUserInfoDBManager(){
        if(dbManager == null)
            dbManager = DatabaseManager.getUserInfoDBManager();
    }


    static synchronized BusinessInterface.IUserInfoManager getInstance(){
        if(instance == null)
            instance = new UserInfoManager();
        return instance;
    }

    public static void clearCurrentUserInfo() {
        SharedPrefrencesUtil.clearKey(SharedPreferencesKey.KEY_USER_ID);
        SharedPrefrencesUtil.clearKey(SharedPreferencesKey.KEY_USER_NAME);
        SharedPrefrencesUtil.clearKey(SharedPreferencesKey.KEY_USER_TYPE);
        SharedPrefrencesUtil.clearKey(SharedPreferencesKey.KEY_IS_VISITOR);
    }

    public static String getCurrentAccount() {
        return (String) SharedPrefrencesUtil.get(SharedPreferencesKey.KEY_ACCOUNT, null);
    }

    public static void saveCurrentAccount(String account) {
        SharedPrefrencesUtil.put(SharedPreferencesKey.KEY_ACCOUNT, account);
    }

    public static String getCurrentUserId() {
        return (String) SharedPrefrencesUtil.get(SharedPreferencesKey.KEY_USER_ID, null);
    }

    public static void saveCurrentUserId(String userId) {
        SharedPrefrencesUtil.put(SharedPreferencesKey.KEY_USER_ID, userId);
    }

    public static String getCurrentName() {
        return (String) SharedPrefrencesUtil.get(SharedPreferencesKey.KEY_USER_NAME, null);

    }

    public static void saveCurrentName(String name) {
        SharedPrefrencesUtil.put(SharedPreferencesKey.KEY_USER_NAME, name);
    }

    public static int getCurrentUserType() {
        int type = (int) SharedPrefrencesUtil.get(SharedPreferencesKey.KEY_USER_TYPE, USER_TYPE_TEACHER);
        return type == USER_TYPE_TEACHER ? type : USER_TYPE_STUDENT;
    }

    public static void saveCurrentUserType(int userType) {
        SharedPrefrencesUtil.put(SharedPreferencesKey.KEY_USER_TYPE, userType);
    }

    public static String getDepartmentName() {
        return (String) SharedPrefrencesUtil.get(SharedPreferencesKey.KEY_DEPARTMENT_NAME, null);
    }

    public static void saveDepartmentName(String departmentName) {
        SharedPrefrencesUtil.put(SharedPreferencesKey.KEY_DEPARTMENT_NAME, departmentName);
    }

    public static String getClassCode() {
        return (String) SharedPrefrencesUtil.get(SharedPreferencesKey.KEY_CLASS_CODE, null);
    }

    public static void saveClassCode(String classCode) {
        SharedPrefrencesUtil.put(SharedPreferencesKey.KEY_CLASS_CODE, classCode);
    }

    public static String getClassName() {
        return (String) SharedPrefrencesUtil.get(SharedPreferencesKey.KEY_CLASS_NAME, null);
    }

    public static void saveClassName(String className) {
        SharedPrefrencesUtil.put(SharedPreferencesKey.KEY_CLASS_NAME, className);
    }

    public static String getDepartmentCode() {
        return (String) SharedPrefrencesUtil.get(SharedPreferencesKey.KEY_DEPARTMENT_CODE, null);
    }

    public static void saveDepartmentCode(String departmentCode) {
        SharedPrefrencesUtil.put(SharedPreferencesKey.KEY_DEPARTMENT_CODE, departmentCode);
    }

    public static String getAvatar() {
        return (String) SharedPrefrencesUtil.get(SharedPreferencesKey.KEY_AVATAR, null);
    }

    public static void saveAvatar(String avatar) {
        SharedPrefrencesUtil.put(SharedPreferencesKey.KEY_AVATAR, avatar);
    }

//    putblic static String getOAPassword() {
//        return (String) SharedPrefrencesUtil.get(SharedPreferencesKey.KEY_OA_PASSWORD, null);
//    }
//
//    public static void saveOAPassword(String password) {
//        SharedPrefrencesUtil.put(SharedPreferencesKey.KEY_OA_PASSWORD, new MD5().getMD5FromStr(password));
//    }

    public static String getAccessToken() {
        return (String) SharedPrefrencesUtil.get(SharedPreferencesKey.KEY_ACCESS_TOKEN, null);
    }

    public static void saveAccessToken(String token) {
        SharedPrefrencesUtil.put(SharedPreferencesKey.KEY_ACCESS_TOKEN, token);
    }

    public static boolean isVisitor() {
        return (boolean) SharedPrefrencesUtil.get(SharedPreferencesKey.KEY_IS_VISITOR, false);
    }

    public static void saveIsVisitor(boolean isVisitor) {
        SharedPrefrencesUtil.put(SharedPreferencesKey.KEY_IS_VISITOR, isVisitor);
    }

    public static void saveGesturePassword(String password){
        SharedPrefrencesUtil.putAboutUser(getCurrentUserId(), SharedPreferencesKey.KEY_GESTURE_PASSWORD, password);
    }

    public static String getGesturePassword(){
        return (String) SharedPrefrencesUtil.getAboutUser(getCurrentUserId(), SharedPreferencesKey.KEY_GESTURE_PASSWORD, null);
    }

    public static void clearGesturePassword(){
        SharedPrefrencesUtil.clearKeyAboutUser(getCurrentUserId(), SharedPreferencesKey.KEY_GESTURE_PASSWORD);
    }

    public static void setModifyPasswordFlag(boolean flag) {
        String userID = getCurrentUserId();
        SharedPrefrencesUtil.putAboutUser(userID, SharedPreferencesKey.KEY_MODIFY_PASSWORD_FLAG, flag);
    }

    public static boolean getModifyPasswordFlag() {
        return true;
//        String userID = getCurrentUserId();
//        return (boolean) SharedPrefrencesUtil.getAboutUser(userID, SharedPreferencesKey.KEY_MODIFY_PASSWORD_FLAG, false);
    }

    public static void saveRongyunToken(String token){
        String userID = getCurrentUserId();
        SharedPrefrencesUtil.putAboutUser(userID, SharedPreferencesKey.KEY_RONGYUN_TOKEN, token);
    }

    public static String getRongyunToken(){
        String userID = getCurrentUserId();
        return (String) SharedPrefrencesUtil.getAboutUser(userID, SharedPreferencesKey.KEY_RONGYUN_TOKEN, null);
    }

    public UserInfo getUserBaseInfo(String userId){
        initUserInfoDBManager();
        return dbManager.getUserBaseInfo(userId);
    }

    @Override
    public StudentInfo getStudentInfoLocal(String userId) {
        initUserInfoDBManager();
        return dbManager.getStudentInfo(userId);
    }

    @Override
    public void getStudentInfoRemote(String userId, final BusinessInterface.OnBusinessResultListener listener) {
        Map<String, Object> map = new HashMap<>();
        map.put("userID", userId);
        HttpClient.get(URL_GET_STUDENT_INFO, map, StudentInfo.class, new HttpClient.OnHttpResultListener<StudentInfo>(){

            @Override
            public void onResult(boolean isSuccess, List<StudentInfo> data, String msg) {
                if(isSuccess){
//                    initUserInfoDBManager();
//                    dbManager.saveStudentDetail(data.get(0));
                    if (listener != null)
                        listener.onBusinessResult(true, data.get(0), msg);
                }else{
                    if (listener != null)
                        listener.onBusinessResult(false, null, msg);
                }
            }
        });
    }

    @Override
    public TeacherInfo getTeacherInfoLocal(String userId) {
        initUserInfoDBManager();
        return dbManager.getTeacherInfo(userId);
    }

    @Override
    public void getTeacherInfoRemote(String userId, final BusinessInterface.OnBusinessResultListener listener) {
        Map<String, Object> map = new HashMap<>();
        map.put("userID", userId);
        HttpClient.get(URL_GET_TEACHER_INFO, map, TeacherInfo.class, new HttpClient.OnHttpResultListener<TeacherInfo>(){

            @Override
            public void onResult(boolean isSuccess, List<TeacherInfo> data, String msg) {
                if(isSuccess){
//                    initUserInfoDBManager();
//                    dbManager.saveTeacherDetail(data.get(0));
                    if (listener != null)
                        listener.onBusinessResult(true, data.get(0), msg);
                }else{
                    if (listener != null)
                        listener.onBusinessResult(false, null, msg);
                }
            }
        });
    }

    @Override
    public List<StudentContactInfo> getStudentList() {
        initUserInfoDBManager();
        return dbManager.getStudentContactList();
    }

    @Override
    public void getStudentListRemote(final BusinessInterface.OnBusinessResultListener listener) {
        HttpClient.get(URL_GET_STUDENT_LIST, null, StudentContactInfo.class, new HttpClient.OnHttpResultListener<StudentContactInfo>(){

            @Override
            public void onResult(boolean isSuccess, List<StudentContactInfo> data, String msg) {
                if(isSuccess){
                    initUserInfoDBManager();
                    dbManager.saveStudentContactList(data);
                    if(listener != null){
                        listener.onBusinessResult(true, data, msg);
                    }
                }else{
                    if(listener != null){
                        listener.onBusinessResult(false, false, msg);
                    }
                }
            }
        });
    }

    @Override
    public List<TeacherContactInfo> getTeacherList() {
        initUserInfoDBManager();
        return dbManager == null ? null : dbManager.getTeacherContactList();
    }

    @Override
    public void getTeacherListRemote(final BusinessInterface.OnBusinessResultListener listener) {
        HttpClient.get(URL_GET_TEACHER_LIST, null, TeacherContactInfo.class, new HttpClient.OnHttpResultListener<TeacherContactInfo>(){

            @Override
            public void onResult(boolean isSuccess, List<TeacherContactInfo> data, String msg) {
                if(isSuccess){
                    initUserInfoDBManager();
                    dbManager.saveTeacherContactList(data);
                    if(listener != null){
                        listener.onBusinessResult(true, data, msg);
                    }
                }else{
                    if(listener != null){
                        listener.onBusinessResult(false, false, msg);
                    }
                }
            }
        });
    }

    public List<StudentInfo> getStudentListByClassNum(String class_num){
        initUserInfoDBManager();
        return dbManager == null ? null : dbManager.getStudentListByClassNum(class_num);
    }


}
